<?php

namespace app\controller;

use Exception;
use think\db\exception\DbException;
use think\facade\Db;
use think\facade\Request;

class Bind
{
    # ===== 获取绑定码 =====
    public function get_bind_code()
    {
        try {
            $ip = Request::header('CF-Connecting-IP');
            $time = date('Y-m-d H:i:s', time());
            #清理过期的绑定码,删除bind_code表中time_out小于当前时间的记录
            try {
                Db::table('bind_code')->where('time_out', '<', $time)->delete();
            } catch (DbException $e) {
            }
            $bind_code = (md5($time . rand(10000, 99999)));
            $time_out = date('Y-m-d H:i:s', time() + 300);
            #将生成的绑定码，ip，过期时间，创建一条新记录，插入bind_code表中,创建一条新记录
            Db::table('bind_code')->insert(['bind_code' => $bind_code, 'ip' => $ip, 'time_out' => $time_out]);
            #返回绑定码
            return (returnjson(0, 'success', data: ["bind_code" => $bind_code, "time_out" => $time_out]));
        } catch (Exception $e) {
            return (returnjson(500, '获取绑定码失败', $e->getMessage()));
        }
    }

    public function check_alive()
    {
        return (returnjson(200, 'alive'));
    }

    # ===== 验证qq号是否在微服群，，微服群员私聊情景专用 =====
    public function check_qq_in_group()
    {
        try {
            $data = Request::param();
            $qq = $data['qq'];
            $qg = $data['qg'];
            $r = Db::table('qq0')->where('member', $qq)->where('qg', $qg)->find();
            if ($r == null or $r == [] or $r == ''){
                log_other('QQ号' . $qq . '不在微服内,试图访问机器人');
                return (returnJson(201, 'QQ号' . $qq . '不在微服内'));
            }
            return (returnjson(0, '该QQ号' . $qq . '在微服内'));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }


    }

    # ===== 绑定bindID =====
    public function bind_qq()
    {
        try {
            $data = Request::param();
            $operator = $data['operator'];
            $qq = $data['qq'];
            $bind_code = $data['bind_code'];
            $time = date('Y-m-d H:i:s', time());
            $r = Db::table('bind_code')->where('bind_code', $bind_code)->whereTime('time_out', '>', $time)->find();
            if (!$r) return (returnjson(msg: '绑定码无效或已过期'));
            $black_list = Db::table('black_list')->where(['qq' => $qq, 'state' => 1])->find();
            if ($black_list){
                $msg = '已被封禁的QQ用户'.$qq.'尝试绑定';
                Db::table('log_other')->insert(['msg'=>$msg]);
                return (returnjson(msg:'您处于黑名单中，不能绑定'));
            }
            $new_ip = $r['ip'];
            $m = Db::table('bound_list')->where(['qq' => $qq, 'ip' => $new_ip])->find();
            if ($m) {
                return (returnjson(msg:'已绑定,不用再绑了'));
            }
            Db::table('bound_list')->insert(['qq' => $qq, 'ip' => $new_ip, 'bound_time' => date('Y-m-d H:i:s', time())]);
            $x = Db::table('bound_list')->where('qq', $qq)->select();
            if (count($x) > 3) {
                $j = Db::table('bound_list')->where('qq', $qq)->order('bound_time', 'desc')->limit(2, 1)->select();
                Db::table('bound_list')->where('qq', $qq)->where('bound_time', '<', $j[0]['bound_time'])->delete();
            }
            Db::table('bind_code')->where('bind_code', $bind_code)->delete();
            return (returnjson(msg: '绑定成功'));

        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }
    }

    # ===== 查绑定状态 =====
    public function check_bound_qq()
    {
        try {
            $data = Request::param();
            $qq = intval($data['qq']);

            if (!$qq) {
                return (returnjson(msg: 'QQ参数错误'));
            }
            $r = Db::table('bound_list')->where('qq', $qq)->select();
            if (!$r) return (returnjson(201, 'QQ号' . $qq . '未绑定'));
            return (returnjson(msg: 'QQ号' . $qq . '拥有绑定信息', data: $r));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }
    }

    public function check_bound_ip()
    {
        try {
            $data = Request::param();
            $ip = $data['ip'];
            $r = Db::table('bound_list')->where('ip1', $ip)->whereOr('ip2', $ip)->whereOr('ip3', $ip)->find();
            if ($r == null) return (returnjson(201, 'IP' . $ip . '未绑定'));
            $data = ['qq' => $r['qq'], 'ip1' => $r['ip1'], 'ip2' => $r['ip2'], 'ip3' => $r['ip3'], 'bound_time1' => $r['bound_time1'], 'bound_time2' => $r['bound_time2'], 'bound_time3' => $r['bound_time3']];
            if ($data == null) return (returnjson(201, '数据库好像出问题了喵', $data));
            return (returnjson(msg:'IP' . $ip . '已绑定', data:$data));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }
    }

    #通过nick_name查绑定信息
    public function check_bound_game_name()
    {
        try {
            $data = Request::param();
            $game_name = $data['game_name'];
            $r = Db::table('bound_list')->where('nick_name', $game_name)->select();
            if ($r == null) {
                #该nick_name未绑定，返回false
                return (returnjson(201, '该昵称未绑定'));
            }
            $r = Db::table('bound_list')->where('nick_name', $game_name)->select();
            $data = array();
            foreach ($r as $value) {
                $r = array_push($date, $value['qq']);
            }
            return (returnjson(msg:'该昵称' . $game_name . '已绑定',data: $r));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }
    }

    # ===== 解除绑定 =====
    public function delete_bound()
    {
        try {
            $data = Request::param();
            $operator = $data['operator'];
            $qq = $data['qq'];
            if ($qq != $operator) {
                return (returnjson(201, '操作对象qq参数错误'));
            }
            Db::table('bound_list')->where('qq', $qq)->delete();
            return (returnjson(msg: '操作成功'));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }

    }

    public function check_ip()
    {
        try {
            $data = Request::param();
            $ip = $data['ip'];
            #查询bound_list中是否存在该ip，若存在，返回true，否则返回false
            $r = Db::table('bound_list')->where('ip', $ip)->find();
            if ($r == null) return (returnjson(201, '该IP未绑定'));
            return (returnjson(msg:'该IP已绑定', data:$r));
        } catch (Exception $e) {
            return (returnjson(500, '服务器错误', $e->getMessage()));
        }

    }


}
